package com.example.tests;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import com.example.obj.GroupObject;
import com.example.obj.GroupSet;

public class Test_01_GroupCreation extends TestBase{
	
	@Test(priority = 1, dataProvider = "groupsFromFile", dataProviderClass = GroupDataGenerator.class)
	public void testGroupCanBeCreated(GroupObject validGroup, boolean valid) throws Exception {
		log.info("Start testGroupCanBeCreated");
		GroupSet oldGroupSet = manager.getGroupHelper().getGroupsCouche();
		log.info("Old groups list: " + oldGroupSet);
		manager.getGroupHelper().createGroup(validGroup);		
		GroupSet newGroupSet = manager.getGroupHelper().getGroupsCouche();
		log.info("New groups list: " + newGroupSet);
		log.info("Start assertions: size, lists' elements");
		if (valid) {			
			assertThat(newGroupSet.size(), equalTo(oldGroupSet.size() + 1));
			assertThat(newGroupSet, equalTo(oldGroupSet.with(validGroup)));
		}
		else{
			assertThat(newGroupSet.size(), equalTo(oldGroupSet.size()));
			assertThat(newGroupSet, equalTo(oldGroupSet));		
		}
		log.info("Stop assertions");
	}
	
	@AfterMethod
	public void testUIConfirmWithDB(){
		log.info("After testGroupCanBeCreated");
		GroupSet groupsUI = manager.getGroupHelper().getGroupsUI();
		log.info("Groups from UI: " + groupsUI);
		GroupSet groupsDB = manager.getGroupHelper().getGroupsCouche();
		log.info("Groups from DB: " + groupsDB);
		assertThat(groupsDB, equalTo(groupsUI));
	}
}
